package com.chs.recoginition.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chs.recoginition.core.exception.ApplicationException;
import com.chs.recoginition.core.exception.constant.ExceptionConstant;
import com.chs.recoginition.core.utils.AssertHelper;
import com.chs.recoginition.core.utils.BeanMapper;
import com.chs.recoginition.core.utils.UploadUtil;
import com.chs.recoginition.mapper.AndroidAppInfoMapper;
import com.chs.recoginition.pojo.AndroidAppInfo;
import com.chs.recoginition.service.AndroidAppInfoService;
import com.chs.recoginition.vo.AndroidAppDownloadVo;
import com.chs.recoginition.vo.AndroidAppSaveVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * <p>
 * 安卓 服务实现类
 * </p>
 * seckill
 * @author CHS
 * @since 2022-07-23
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class AndroidAppInfoServiceImpl extends ServiceImpl<AndroidAppInfoMapper, AndroidAppInfo>
        implements AndroidAppInfoService {

    @Override
    public void uploadApp(HttpServletResponse response, AndroidAppSaveVo saveVo) throws IOException {
        AssertHelper.notNull(saveVo, "上传文件不能为空");
        MultipartFile uploadFile = saveVo.getFile();
        String filePath = UploadUtil.uploadFile(uploadFile, "androidFile", "androidFile");
        filePath = UploadUtil.getRootPath().concat(filePath);
        AndroidAppInfo entity = BeanMapper.map(saveVo, AndroidAppInfo.class);
        entity.setFilePath(filePath);
        this.save(entity);
    }

    @Override
    public void downloadApp(HttpServletResponse response, String appName, Integer version) throws FileNotFoundException {
        AndroidAppInfo androidAppInfo = baseMapper.selectByAppNameVersion(appName, version);
        if (androidAppInfo == null) {
            throw new ApplicationException(ExceptionConstant.ANDROID_APP_NOT_EXISTS_ERROR);
        }
        File file = new File(androidAppInfo.getFilePath());
        response.setHeader("content-type", "application/octet-stream");
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment;filename=" + androidAppInfo.getApkName() + ".apk");
        byte[] buffer = new byte[1024];
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        try {
            fis = new FileInputStream(file);
            bis = new BufferedInputStream(fis);
            ServletOutputStream outputStream = response.getOutputStream();
            int i = bis.read(buffer);
            while (i != -1) {
                outputStream.write(buffer, 0, i);
                i = bis.read(buffer);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public AndroidAppDownloadVo selectLastVersion(String appName) {
        AndroidAppInfo appInfo = baseMapper.selectLastVersion(appName);
        Integer version = appInfo.getVersion();
        String url = "api/android/download".concat("?appName=").concat(appName)
                .concat("&version=").concat(version.toString());
        return new AndroidAppDownloadVo(version, appName, url);
    }
}
